package ac.svn.accesscontrol.ui;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JList;
import javax.swing.SwingUtilities;

import ac.svn.accesscontrol.manager.SVNInfoManager;
import ac.svn.accesscontrol.store.AccessUnit;
import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.store.SVNGroup;
import ac.svn.accesscontrol.store.SVNLocation;
import ac.svn.accesscontrol.store.SVNUser;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;

/**
 * 
 */

/**
 * @author Enigmanic
 *
 */
public class LocationFilterByUser 
{
	private String searchCriteria = "";
	private Thread listerThread = null;
	private JList targetListView = null;
	private ThreadedListModel listModel = null;
	private Set<ISVNItem> targetList = null;
	private boolean groupFiltered = false;
	private boolean userFiltered	    = false;

	public LocationFilterByUser(String componentId,String modelId)
	{
		targetListView = (JList) UIComponentLibrary.getComponent(componentId);
		listModel = (ThreadedListModel) UIComponentLibrary.getComponent(modelId); 

		listerThread = new Thread(new Runnable() {

			@Override
			public void run() {
				//Clear list before populating it.
				listModel.clear();
				
				if(true == groupFiltered)
				{
					filterByGroup();
				}
				
				if(true == userFiltered)
				{
					filterByUser();
				}
				
				if((false == groupFiltered) && (false == userFiltered))
				{
					for(ISVNItem isvnItem : targetList)
					{
						listModel.addElement(isvnItem);
					}
				}
				
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						SwingUtilities.updateComponentTreeUI(targetListView);						
					}
				});				
			}

			private void filterByUser()
			{
				SVNUser svnUser = SVNInfoManager.getInfoManager().getSvnUser(searchCriteria);
				Set<String> memberShips = new HashSet<String>();

				//svnUser.addGroup(searchCriteria.trim());
				for(String groupAlias : svnUser.getGroups())
				{
					memberShips.add(groupAlias.trim());
				}

				for(ISVNItem item : targetList)
				{
					SVNLocation svnLocation = (SVNLocation) item;
					for(String accessUnitId : svnLocation.getAccessUnits())
					{
						AccessUnit  accessUnit = SVNInfoManager.getInfoManager().getAccessUnit(accessUnitId);
						if(memberShips.contains(accessUnit.getOwnerId().trim()))
						{
							listModel.addElement(svnLocation);
						}
					}
				}
			}

			private void filterByGroup()
			{
				try
				{
					SVNGroup svnGroup = SVNInfoManager.getInfoManager().getGroup(searchCriteria);
					for(ISVNItem item : targetList)
					{
						SVNLocation svnLocation = (SVNLocation) item;
						for(String accessUnitId : svnLocation.getAccessUnits())
						{
							AccessUnit  accessUnit = SVNInfoManager.getInfoManager().getAccessUnit(accessUnitId);
							if(svnGroup.getGroupAlias().equalsIgnoreCase(accessUnit.getOwnerId()))
							{
								listModel.addElement(svnLocation);
							}
						}
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
					System.err.println("Filter by group caused application to crash!");
				}
			}

		});		
	}

	public void filterListByUser(String userId)
	{
		groupFiltered = false;
		searchCriteria = userId;
		resetThread();
		targetList = SVNInfoManager.getInfoManager().getAllLocations();
		filter();
	}

	public void filterListByGroup(String groupId)
	{
		groupFiltered = true;
		searchCriteria = groupId;
		resetThread();
		targetList = SVNInfoManager.getInfoManager().getAllLocations();
		filter();
	}

	private void resetThread()
	{
		if(listerThread.isAlive())
		{
			listerThread.interrupt();
		}
	}

	private void filter()
	{
		listerThread.run();
	}
	
	public void listAll()
	{
		groupFiltered = false;
		userFiltered = groupFiltered;
		searchCriteria ="";
		resetThread();
		targetList = SVNInfoManager.getInfoManager().getAllLocations();
		filter();
	}

}
